/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author kien.tran
 */
public class ListItem extends AListItem {

    private int paddingTop = 0;

    public ListItem(String id, String text) {
        super(text, null);
        super.id = id;
    }

    public ListItem(String id, String text, Image icon) {
        super(text, icon);
        super.id = id;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }
        // pre-calc
        calculateHeight();
        setTextRows(text);

        int tempX, tempY;
        tempX = x + style.getPadding().getLeft();
        tempY = y + style.getPadding().getTop();
        if (focus) {
            // paint background
            g.setColor(style.getSelectedBgColor());
            g.fillRect(x, y, width, height);

            if (icon != null) {
                g.drawImage(icon, tempX, tempY, Graphics.TOP | Graphics.LEFT);
                tempX += icon.getWidth() + style.getPadding().getLeft();
            }

            tempY += paddingTop;

            // paint text
            for (int i = 0; i < textRows.length; i++) {
                font.getFontNormal().drawString(g, textRows[i], tempX, tempY + i * (font.getFontNormal().getHeight() + style.getRowHeight()), Graphics.TOP | Graphics.LEFT);
            }

        } else {
            if (icon != null) {
                g.drawImage(icon, tempX, tempY, Graphics.TOP | Graphics.LEFT);
                tempX += icon.getWidth() + style.getPadding().getLeft();
            }

            tempY += paddingTop;
            
            // paint first row
            font.getFontNormal().drawString(g, textRows[0], tempX, tempY, Graphics.TOP | Graphics.LEFT);
        }
    }

    protected void calculateHeight() {
        int textH = 0, iconH = 0;

        if (icon != null) {
            iconH = icon.getHeight() + style.getPadding().getTopAndBottom();
        }

        if (textRows == null) {
            setTextRows(text);
        }

        if (focus) {
            textH = textRows.length * font.getFontNormal().getHeight() + (textRows.length - 1) * style.getRowHeight() + style.getPadding().getTopAndBottom();
        } else {
            textH = font.getFontNormal().getHeight() + style.getPadding().getTopAndBottom();
        }

        if (iconH > textH) {
            paddingTop = (iconH - textH) / 2;
            style.setHeight(iconH);
        } else {
            paddingTop = 0;
            style.setHeight(textH);
        }

        this.height = style.getHeight();
    }

    protected void setTextRows(String text) {
        if (text.length() > 0) {
            int w = 0;
            style.setWidth(width);
            if (icon != null) {
                w = width - icon.getWidth() - style.getPadding().getLeft() - style.getPadding().getLeftAndRight();
            } else {
                w = width - style.getPadding().getLeftAndRight();
            }
//            textRows = normalFont.splitWith(text, w);
            textRows = new String[]{text};
        } else {
            textRows = new String[]{""};
        }
    }
}
